<template>
  <div class="channel-edit">
    <van-cell title="我的频道" :border="false">
      <van-button @click="isEdit = !isEdit" color="red" class="edit-btn" plain type="primary" size="small" round >
        {{ isEdit ? '完成' : '编辑' }}
      </van-button>
    </van-cell>
    <van-grid :gutter="10" class="my-grid" :border="false">
      <van-grid-item
      :class="{active: index === active}"
      :text="item.name"
      :icon="isEdit && index !==0 ? 'close' : ''"
      v-for="(item, index) in channel"
      :key="item.id"
      @click="delItem(index, item.id)"
      />
    </van-grid>

    <van-cell title="推荐频道" :border="false"></van-cell>
    <van-grid direction="horizontal" icon-size="0.35rem" :gutter="10" class="my-grid" :border="false" >
      <van-grid-item
      @click="onRecommendChannels(item)"
      v-for="item in recommendChannels"
      :key="item.id"
      icon="plus"
      :text="item.name" />
    </van-grid>
  </div>
</template>

<script>
import { setStorage } from '@/utils/storage.js'
import { getAllChannel, addUserChannelsItem, removeUserChannelsItem } from '@/api/article.js'
import { Toast } from 'vant'
import { mapState } from 'vuex'
export default {
  props: {
    channel: {
      type: Array,
      required: true
    },
    // 记录的高亮的索引
    active: {
      type: Number,
      required: true
    }
  },
  data () {
    return {
      isEdit: false,
      allChannels: []

    }
  },

  created () {
    this.loadAllChannel()
  },

  computed: {
    ...mapState(['user']),
    recommendChannels () {
    // 对allChannels进行筛选
    // allChannels的每一项 是否在channels里,如果没有在channels,属于recommendChannels
    // const arr = this.allChannels.filter(item => {
    //   const flag = this.channels.some(item2 => {
    //     return item2.id === item.id
    //   })
    //   return !flag
    // })
    // return arr
      return this.allChannels.filter(item => {
        return !this.channel.some(item2 => item2.id === item.id)
      })
    }
  },

  methods: {
    // 点击了推荐频道
    async onRecommendChannels (obj) {
      // 只操作了页面显示
      this.channel.push(obj)

      Toast.loading({
        message: '添加中...',
        forbidClick: true,
        duration: 0 // 持续展示 toast
      })
      // 进行数据持久化
      if (this.user) {
        // 登录
        try {
          await addUserChannelsItem({
            id: obj.id,
            seq: this.channel.length
          })
          Toast.success('添加成功')
        } catch (err) {
          Toast.fail('添加失败')
        }
      } else {
        setStorage('MY-CHANNEL', this.channel)
        Toast.success('添加成功')
      }
    },

    delItem (index, id) {
      if (this.isEdit) {
        // 处于编辑状态
        if (index === 0) return
        if (index <= this.active) {
          this.$emit('setActive')
        }
        this.channel.splice(index, 1)
        // 点击我的频道
        this.delChannelStorage(id)
      } else {
        // 处于非编辑状态
        this.$emit('onGo', index)
      }
    },

    // 删除持久化
    async delChannelStorage (id) {
      // 未登陆

      if (!this.user) return setStorage('MY-CHANNEL', this.channel)
      // 已登录
      Toast.loading({
        message: '删除中...',
        forbidClick: true,
        duration: 0 // 持续展示 toast
      })
      try {
        await removeUserChannelsItem(id)
        Toast.success('删除成功')
      } catch (err) {
        Toast.fail('删除失败')
      }
    },

    async loadAllChannel () {
      try {
        const res = await getAllChannel()
        this.allChannels = res.channels
      } catch (err) {
        Toast('获取所有频道成功')
      }
    }

  }
}
</script>

<style scoped lang='less'>
.channel-edit {
  .edit-btn {
    width: 104px;
    height: 48px;
  }

  /deep/.my-grid {
    .active{
    .van-grid-item__text{
      color: orangered;
    }
    }
    .van-grid-item {
      width: 160px;
      height: 86px;
      border-radius: 6px;
    }
      .van-grid-item__content {
      background: #F4F5F6;
      }
      .van-icon-close {
      font-size: 35px;
      position: absolute;
      top: -10.5px;
      right: 5px;
    }
    .van-grid-item__icon+.van-grid-item__text {
      margin-top: 0;
    }

  }

}

</style>
